<template>
  <div style="width: 100%; height: 100%">
    <div id="chart-pie" ref="chart-pie"></div>
  </div>
</template>

<script>
import * as echarts from "echarts/lib/echarts";
import { PieChart } from "echarts/charts";
import {
  TooltipComponent,
  LegendComponent,
  GridComponent,
  DataZoomComponent,
  TitleComponent,
  GraphicComponent,
} from "echarts/components";
echarts.use([
  PieChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  DataZoomComponent,
  TitleComponent,
  GraphicComponent,
]);
import { debounce } from "@/utils";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      chart: null,
    };
  },
  computed: {
    ...mapGetters(["autoResize"]),
  },
  watch: {
    autoResize: {
      handler: debounce(function () {
        this.chart.resize();
      }, 150),
    },
  },
  created() {
    this.$nextTick(() => {
      this.chart = echarts.init(this.$refs["chart-pie"]);
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart.setOption({
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "40",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: 1048, name: "Search Engine" },
              { value: 735, name: "Direct" },
              { value: 580, name: "Email" },
              { value: 484, name: "Union Ads" },
              { value: 300, name: "Video Ads" },
            ],
          },
        ],
      });
    },
  },
};
</script>
<style scoped>
#chart-pie {
  width: 100%;
  height: 100%;
}
</style>